﻿Imports System.IO

Public Class Utility

    Public Class Proprieta
        Public strNomeCampo As String
        Public strValore As String
        Public oType As Type

        Public Sub New(ByVal inNamecampo As String, ByVal inValore As String, ByVal inType As Type)
            strNomeCampo = inNamecampo
            strValore = inValore
            oType = inType
        End Sub

    End Class

    Public Shared Function CalcolaGiorni(ByVal intMese As Integer, ByVal intAnno As Integer) As Integer()
        Try
            Dim vettGiorni(6) As Integer
            For i As Integer = 1 To 7
                Dim tipoGiorno As Integer = Weekday(DateValue(i & "/" & intMese & "/" & intAnno), vbMonday) - 1
                Dim j As Integer = i

                vettGiorni(tipoGiorno) = 0
                Do While Not j > Date.DaysInMonth(intAnno, intMese)
                    vettGiorni(tipoGiorno) += 1
                    j += 7
                Loop
            Next i

            Return vettGiorni
        Catch ex As Exception
            Base.ShowError.Show("Previsioni", "Utility", "CalcolaGiorni", ex)
            Return Nothing
        End Try
    End Function

    Public Shared Function FormatSqlDecimal(ByVal inValore As Decimal) As String
        Try
            Return CStr(inValore).Replace(",", ".")
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Shared Function ConvStrNum(ByVal inString As String) As Decimal
        Try
            If IsNumeric(inString) Then
                Return inString.Replace(".", ",")
            Else
                Return 0
            End If

        Catch ex As Exception
            Return 0
        End Try
    End Function

    Public Shared Function FormatSqlData(ByVal inData As String) As String
        Try
            If IsDate(inData) Then
                If CDate(inData).Year.ToString <> Mid(inData, 1, 4) Then
                    Dim dData As DateTime = CType(inData, DateTime)
                    Return String.Format("{0}/{1}/{2}", dData.Year.ToString, dData.Month.ToString, dData.Day.ToString)
                Else
                    Return inData
                End If
            Else
                Return ""
            End If

        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Shared Function IsFormOpened(ByVal inNameForm As String) As Boolean
        For Each oFrm As Form In My.Application.OpenForms
            If oFrm.Name = inNameForm Then
                Return True
            End If
        Next
        Return False
    End Function

    Public Shared Function GetUltimoGiorno(ByVal intMese As Integer, ByVal intAnno As Integer) As String
        Try
            Select Case intMese
                Case 2
                    Dim blnIsAnnoBisestile As Boolean
                    Dim intGiorniFebbraio As Integer

                    blnIsAnnoBisestile = IsDate(String.Format("{0}/02/29", intAnno))
                    If blnIsAnnoBisestile Then
                        intGiorniFebbraio = 29
                    Else
                        intGiorniFebbraio = 28
                    End If
                    Return intGiorniFebbraio & "/" & intMese & "/" & intAnno
                Case 1, 3, 5, 7, 8, 10, 12
                    Return "31/" & intMese & "/" & intAnno
                Case Else
                    Return "30/" & intMese & "/" & intAnno
            End Select
        Catch ex As Exception
            Base.ShowError.Show("Calcolo giorno", "Utility", "GetUltimoGiorno", ex)
            Return ""
        End Try
    End Function

    Public Shared Function RicaricaListBox(ByVal inQuery As String, ByVal inNameCampo As String, ByRef inLst As ListBox) As Boolean
        Try

            Using daAdapt As New MySqlDataAdapter(inQuery, gConnectionStringAgenda.ToString)
                Using dtTable As New DataTable("")
                    daAdapt.Fill(dtTable)
                    inLst.DataSource = dtTable.Copy
                    inLst.DisplayMember = inNameCampo
                    inLst.ValueMember = "ID"
                End Using
            End Using

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Carica dati", "Utility", "RicaricaListBox", ex)
            Return False
        End Try
    End Function

    Public Shared Function GetNome(ByVal inNameTable As String, ByVal inId As Integer, Optional ByVal inNameCampo As String = "Nome") As String
        Try
            Dim strSql As String = String.Format("Select {2} From {0} Where ID={1}", inNameTable, inId, inNameCampo)
            Using cnConnection As New MySqlConnection(gConnectionStringAgenda.ToString)
                cnConnection.Open()
                Using cmCommand As New MySqlCommand(strSql, cnConnection)
                    Return cmCommand.ExecuteScalar
                End Using
            End Using
        Catch ex As Exception
            Base.ShowError.Show("GetNome", "Utility", "GetNome", ex)
            Return False
        End Try
    End Function

    Public Shared Function IsEnabledElencoDate(ByVal inQuery As String) As Boolean
        Try
            Using cnConnection As New MySqlConnection(gConnectionStringAgenda.ToString)
                cnConnection.Open()
                Using cmCommand As New MySqlCommand(inQuery, cnConnection)
                    Return CBool(Utility.ConvStrNum(cmCommand.ExecuteScalar.ToString))
                End Using
            End Using
        Catch ex As Exception
            Base.ShowError.Show("Elenco Date", "Utility", "IsEnabledElencoDate", ex)
            Return False
        End Try
    End Function

    Public Shared Function GestioneModifiche(ByRef objOld As Object, ByRef objNew As Object) As List(Of Proprieta)
        Try
            Dim listOld As New List(Of Proprieta)
            Dim listNew As New List(Of Proprieta)
            Dim listModifyti As New List(Of Proprieta)


            For Each oFieldInfo As Reflection.FieldInfo In objOld.GetType.GetFields
                listOld.Add(New Proprieta(oFieldInfo.Name, CStr(oFieldInfo.GetValue(objOld)), oFieldInfo.FieldType))
            Next
            For Each oFieldInfo As Reflection.FieldInfo In objNew.GetType.GetFields
                listNew.Add(New Proprieta(oFieldInfo.Name, CStr(oFieldInfo.GetValue(objNew)), oFieldInfo.FieldType))
            Next

            For i As Integer = 0 To listOld.Count - 1
                If listOld(i).oType = GetType(Decimal) Then
                    If CDec(listOld(i).strValore) <> CDec(listNew(i).strValore) Then
                        listModifyti.Add(New Proprieta(listOld(i).strNomeCampo.Substring(1), listOld(i).strValore, Nothing))
                    End If
                ElseIf IsDate(listOld(i).strValore) Then
                    If CDate(listOld(i).strValore) <> CDate(listNew(i).strValore) Then
                        listModifyti.Add(New Proprieta(listOld(i).strNomeCampo.Substring(1), listOld(i).strValore, Nothing))
                    End If
                Else
                    If listOld(i).strValore <> listNew(i).strValore Then
                        listModifyti.Add(New Proprieta(listOld(i).strNomeCampo.Substring(1), listOld(i).strValore, Nothing))
                    End If
                End If
            Next
            Return listModifyti

            listModifyti = Nothing
            listNew = Nothing
            listOld = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Gestione Modifiche", "Utility", "GestioneModifiche", ex)
            Return Nothing
        End Try
    End Function

    Public Shared Sub ConvertImage(ByVal Filename As String, ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, ByVal NewFilename As String)
        ' Takes a filename and saves the file in a new format
        Try
            Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromFile(Filename)
            imgFile.Save(NewFilename, DesiredFormat)
        Catch ex As Exception
            Base.ShowError.Show("ConvertImage", "Utility", "ConvertImage", ex)
        End Try
    End Sub

    Public Shared Function CaricaImpostazioni() As Boolean
        Try
            gIsServer = GetSetting(Application.ProductName, "Impostazioni", "Server", 1)
            If Not gIsServer Then
                gIndirizzoIP = GetSetting(Application.ProductName, "Impostazioni", "IndirizzoIP", "")
            End If
            gstrNomeDB = GetSetting(Application.ProductName, "Impostazioni", "Istanza", "")

            gConnectionStringAgenda = New ConnectionString(If(gIsServer, "localhost", gIndirizzoIP), "root", DbPassword, gstrNomeDB)

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Gestione Modifiche", "Utility", "GestioneModifiche", ex)
            Return False
        End Try
    End Function

End Class
